package com.hgzy.meeting.controller;

import com.hgzy.meeting.model.DepartMent;
import com.hgzy.meeting.model.Employee;
import com.hgzy.meeting.service.DepartMentService;
import com.hgzy.meeting.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.jvm.hotspot.debugger.Page;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class DepartMentController {
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartMentService departmentService;

    /**
     *根据id 查询到员工 employee
     * @param id
     * @return
     */
    @RequestMapping("findId")
    public String findId(int id){
        DepartMent departMent =departmentService.findById(id);
        System.out.println(departMent);
        return "hello controller";
    }

    /**
     * 展示页面需要的部门数据
     * @return
     */
    @RequestMapping("departments")
    public String departments(Model model){
        // 展示页面需要的部门数据
        List<DepartMent> deps = departmentService.findAllDeps();
        model.addAttribute("deps",deps);
        return "departments";
    }

    /**
     * 添加部门
     * @param departmentname
     * @return
     */
    @RequestMapping("adddepartment")
    public String addDepartment(String departmentname,Model model){
        // 确保部门的唯一性 不重复
        DepartMent departMent=departmentService.findDepartment(departmentname);
        if (departMent != null) {
            model.addAttribute("error",departMent.getDepartmentname()+"不能重复");
            return "forward:/admin/departments";
        }
        Integer result=departmentService.addDepartment(departmentname);
        System.out.println(result);
        return "redirect:/admin/departments";
    }


    /**
     * 删除部门
     * @param departmentname
     * @return
     */
    @RequestMapping("deletedepartment")
    public String deleteDepartment(String departmentname){
        Integer result=departmentService.deleteDepartment(departmentname);
        System.out.println(result);
        return "redirect:/admin/departments";
    }

    /**
     * 展示员工数据
     * @param model
     * @return
     */

    private static  final  Integer PAGE_SIZE=10;
    @RequestMapping("searchemployees")
    public String searchemployees(Employee employee,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  Model model){
        List<Employee> emps=employeeService.getAllEmps(employee,page,PAGE_SIZE);
        long total=employeeService.getTotal(employee);
        model.addAttribute("total",total);
        model.addAttribute("page",page);
        model.addAttribute("pagenum",total% PAGE_SIZE==0?total/PAGE_SIZE:(total/PAGE_SIZE)+1);
        model.addAttribute("emps",emps);
        return "searchemployees";
    }


}
